<?php
namespace plugins\setting\lib;
use plugins\setting\model\SettingModel;
use think\Cache;

/**
 * 设置类
 * 基本功能:
 * 1.将文件配置更新到数据库 更新配置到数据库->读取文件配置->筛选新增和删除的配置->执行更新操作->更新缓存
 * 2.缓存已有的设置配置列表 将数据库设置读取到缓存里
 * 3.从缓存列表中读取数据
 */

class Setting 
{
	/**
	 * 读取文件配置
	 */
	public function getSettingConfig()
	{
		$dirs = scandir( PLUGINS_PATH );
		$config  = [
			'list' => [],
			'modules' => []
		];
		foreach ( $dirs as $moduleName ) {
			if( $moduleName == '.' || $moduleName == '..' ){
				continue;
			}
			$moduleName = cmf_parse_name( $moduleName );

			$filePath = PLUGINS_PATH.$moduleName.'/config/setting.json';
		
			if( is_dir( PLUGINS_PATH.$moduleName ) &&  file_exists( $filePath ) ){
				$setting = getModuleConfig( $moduleName , 'config' , 'setting.json' );
				$setting = json_decode( $setting , true ) ;
				if( !empty( $setting ) ){
					$config [ 'list' ] [ $moduleName ] = [
						'name' 		=> $moduleName,
						'title' 	=> isset( $setting [ 'title' ] ) ? $setting [ 'title' ] : '' ,
						'template' 	=> isset( $setting [ 'template' ] ) ? $setting [ 'template' ] : '' ,
					];
					$config [ 'modules' ] [] = $moduleName;
				}
			}
		}
		return $config;
	}

	/**
	 * 将配置更新到数据库
	 */
	public function settingToDb()
	{
		$config = $this->getSettingConfig();

		if( !empty( $config[ 'modules' ] ) ){
			$this->deleteDbSetting( $config[ 'modules' ] );
		}

		if( !empty( $config [ 'list' ] ) ){
			foreach ( $config[ 'list' ] as $item ) {
				$this->insertDbSetting( $item );
			}
		}
	}

	/**
	 * 删除配置
	 */
	private function deleteDbSetting( $moduleNames )
	{
		if( !empty( $moduleNames ) ){
			SettingModel::where( 'name' , 'IN' , $moduleNames )->delete();
		}
		return true;
	}

	/**
	 * 新增配置 
	 */
	private function insertDbSetting( $item )
	{
		$name = $item [ 'name' ];
		$setting = SettingModel::get( [ 'name' => $name ] ) ;
		if( !$setting ){
			$item [ 'title' ] = empty( $item [ 'title' ] ) ? $name : $item [ 'title' ];
			if( isset( $item [ 'data' ] ) && is_array( $item [ 'data' ] ) ){
				$data = json_encode( $item [ 'data' ] , JSON_UNESCAPED_UNICODE  ) ;
				$item [ 'data' ] = empty( $data ) ? [] : $data;
			}
			( new SettingModel() )->allowField( true )->save( $item );
		}else{
			unset( $item [ 'names' ] );
			( new SettingModel() )->where( 'name' , $name )->allowField( true )->isUpdate( true )->save( $item );
		}
		return true;
	}

	/**
	 * 	更新缓存
	 */
	public function updateCache()
	{
		$data = SettingModel::where( 'status' , 1 )->order( 'title' )->select();
		$list = [];
		foreach ($data as $key => $value) {
			$value[ 'data' ] = json_decode( $value [ 'data' ] , true );
			$list [ $value [ 'name' ] ] = $value;
		}
		return $this->setCache( $list );
	}

	/**
	 * 	设置缓存信息
	 */
	public function setCache( $data )
	{
		return Cache::set( '__SYS_SETTING__' , $data );
	}

	/**
	 * 	获取缓存
	 */
	public static function getCache( $index = '' )
	{
		$data = Cache::get( '__SYS_SETTING__' );
		if( empty( $data ) ){
			( new self )->updateCache();
			$data = Cache::get( '__SYS_SETTING__' );
		}
		$indexList = explode( '.' , $index );
		if( !empty( $index ) && is_array( $indexList ) ){
			foreach ( $indexList as $key ) {
				if( isset( $data [ $key ] ) ){
					$data = $data[ $key ];
				}
			}
		}
		return $data;
	}


}
